Skip to content

feat: add prefer-includes-over-regex-test rule - #111

Merged
43081j merged 3 commits into
e18e:mainfrom
webpro:rule-prefer-includes-over-regex-test
May 2, 2026
Merged

43081j merged 3 commits into
e18e:mainfrom
webpro:rule-prefer-includes-over-regex-test

Conversation

@webpro

@webpro webpro commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

New prefer-includes-over-regex-test rule: when a <regex>.test(s) has no metacharacters and no flags, the regex engine work is pure overhead — s.includes('foo') (or startsWith / endsWith / ===) does the same thing without compiling and tearing down a state machine.

A real fix this catches (knip src/plugins/gatsby/index.ts):

// before
if (/gatsby-config/.test(configFileName)) { ... }
if (/gatsby-node/.test(configFileName)) { ... }

// after
if (configFileName.includes("gatsby-config")) { ... }
if (configFileName.includes("gatsby-node")) { ... }

Behaviour

Autofixes the four shapes:

Pattern Replacement Message
/foo/.test(s) s.includes("foo") preferIncludes
/^foo/.test(s) s.startsWith("foo") preferStartsWith
/foo$/.test(s) s.endsWith("foo") preferEndsWith
/^foo$/.test(s) s === "foo" preferEquals

Skips:

  • patterns containing any of \.*+?()[]{}|^$ after stripping anchors
  • regex flags (/i, /g, …) since they change semantics
  • empty bodies (//, /^$/, /^/, /$/)
  • dynamic new RegExp(...).test(...) (could be anything at runtime)
  • .test() with extra arguments

Wraps the test argument in parens when needed to preserve precedence (logical, conditional, await, TS cast, etc.).

Caveat

regex.test(x) coerces x to string; x.includes(…) throws on non-strings. The rule has no way to detect non-string receivers without types, so this can produce broken code for regex.test(maybeNumber). In practice almost all real .test() callers pass strings, but the risk is the reason this lands as opt-in (not in recommended).

Test plan

  • npm test — 706 tests pass (16 in this rule)
  • npm run build
  • npm run lint
  • Ran the rule against knip + 9 cloned consumer codebases. 8 real source hits across 3 codebases (knip 2, rocicorp/mono 5, mapbox-gl-js 1). Sampled 4 — all idiomatic substring tests with no metacharacters; autofix produces clean equivalents. Lower yield than some other rules because most of these codebases already use string methods directly — but the savings per call site are substantial when this pattern does appear.

webpro and others added 2 commits May 1, 2026 15:07
…string methods

`/foo/.test(s)` is the regex engine compiling, executing, and tearing
down a state machine just to ask "does s contain 'foo'?". When the
pattern has no metacharacters or flags, `s.includes('foo')` does the
same thing without the regex machinery.

Autofixes `<RegExpLiteral>.test(s)` whose pattern has no flags and no
metacharacters into the equivalent string method:

- `/foo/.test(s)`     → `s.includes("foo")`
- `/^foo/.test(s)`    → `s.startsWith("foo")`
- `/foo$/.test(s)`    → `s.endsWith("foo")`
- `/^foo$/.test(s)`   → `s === "foo"`

Skips patterns containing any of `\\.*+?()[]{}|^$` (after stripping
anchors), regex flags (`/i`, `/g`, …), empty bodies, dynamic
`RegExp(...)` constructors, and non-string receivers. Wraps the
test argument in parens when it's a logical/conditional/await/etc.
expression to preserve precedence.

Caveat: `regex.test(x)` coerces `x` to string while `x.includes(...)`
throws on non-strings. The rule cannot detect that without types;
shipping it as opt-in.
@43081j
43081j merged commit 78e2f80 into e18e:main May 2, 2026
3 checks passed
@webpro
webpro deleted the rule-prefer-includes-over-regex-test branch May 4, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants